<?php
/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2019/4/16
 * Time: 21:46
 */


$server = new  Swoole\Http\Server('0.0.0.0', 9800);
$server->set(['worker_num' => 1]);

Swoole\Runtime::enableCoroutine(true); //一键协程化

include "pool.php";
$server->on('workerStart', function () {
    Pool::get_instance()->init();
});

//一个协程的数据被另外一个协程修改了
$server->on('request', function ($req, $response) {
    $pool = Pool::get_instance();
    $obj = $pool->getConnection();

    try {
        if ($obj instanceof PDO) {
            $obj->query('select sleep(2);');
        }
        throw  new Exception('超时连接');
    } catch (\Exception $e) {
        //var_dump($e->getMessage());
    }
    $response->end('协程id为' . Co::getcid());
    // var_dump('协程id为'.Co::getcid());
    $pool->freeConnection($obj);
});
$server->start();